// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event) => {
    const {nickName,avatarUrl} = event
    const {OPENID} = cloud.getWXContext()

    const userInfo =cloud.database().collection('user')
    //2.判断用户是否注册
    const{data} = await userInfo.where({
        _openid:OPENID
    }).get()
    if(data.length === 0){
        const {_id} =await userInfo.add({
            data:{
                nickName,
                avatarUrl,
                score:0,
                total:0,
                rate:0,
                allerr:0,
                info:'这家伙太懒了，没写简介',
                _openid:OPENID

            }
                
        })

    const user = await userInfo.doc(_id).get()
    return{
        data:user.data
    }

    }else{
        return{
            data:data[0]
        }
    }



 
}